<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

<!--    <el-empty description="当前页面正在开发中，敬请期待……"></el-empty>-->

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
      <el-table-column prop="username" label="姓名" align="center"></el-table-column>
      <el-table-column prop="nickname"label="昵称" align="center"></el-table-column>
      <el-table-column prop="avatar"label="头像" width="300px" header-align="left" align="center"></el-table-column>
      <el-table-column prop="phone"label="电话" width="120px" align="center"></el-table-column>
      <el-table-column prop="email"label="邮箱" width="180px" align="center"></el-table-column>
      <el-table-column prop="description"label="简介" width="220px" header-align="center" align="left"></el-table-column>

      <el-table-column prop="enable"label="是否启用" align="center">
        <template slot-scope="scope">
        <el-switch
            v-model="scope .row.enable"
            @change="changEnable(scope.row)"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            :disabled="scope.row.id==1"
            inactive-color="#ff4949">
        </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="lastLoginIp"label="登录IP" align="center"></el-table-column>
      <el-table-column prop="loginCount"label="登录次数" align="center"></el-table-column>
      <el-table-column prop="gmtLastLogin"label="最后登录时间" align="center"></el-table-column>
      <el-table-column prop="gmtCreate"label="创建时间" align="center"></el-table-column>
      <el-table-column prop="gmtModified"label="最后修改时间" align="center"></el-table-column>

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit"  :disabled="scope.row.id==1" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" :disabled="scope.row.id==1" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
    }
  },
  methods:{
    changEnable(admin){
    let enableText = ['禁用', '启用'];
      console.log("在localStorage中的JWT是"+localStorage.getItem('jwt'))

      let url = 'http://localhost:9081/admins/' + admin.id;
   if(admin.enable==1){
     console.log('将启用管理员');
     url += '/enable';
   } else {
     console.log('将禁用管理员');
     url += '/disable';
   }
      console.log('url = ' + url);
      this.axios.create({
        'headers':{
          'Authorization':localStorage.getItem('jwt')
        }
      }).post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将管理员【' + admin.username + '】的启用状态改为【' + enableText[admin.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 40400) {
          this.loadAdminList();
        }
      });
    },
    handleEdit(admin) {
      let message = '您正在尝试编辑【' + admin.id + '-' + admin.username + '】的管理员详情，抱歉，该功能尚未实现……';
      this.$alert(message, '提示', {
        confirmButtonText: '确定'
      });
    },
    handleDelect(admin){
      console.log("准备删除"+admin.id)
      let url='http://localhost:9081/admins/'+admin.id+'/delete';
      this.axios.create({
        'headers':{
          'Authorization':localStorage.getItem('jwt')
        }
      }).post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadAdminList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadAdminList();
        }
      })
    },
    openDelectConfirm(admin){
      let message='此操作将永久删除该文件['+admin.username+'], 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(admin);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadAdminList(){
      console.log('loadadminList方法')
      console.log("在localStorage中的JWT是"+localStorage.getItem('jwt'));
      let url='http://localhost:9081/admins';
      console.log('url='+url);
      this.axios.create({
        'headers':{
          'Authorization':localStorage.getItem('jwt')
        }
      }).get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadAdminList();
  }

}
</script>